<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>云备份文件下载</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
            box-sizing: border-box;
            font-family: "PingFang SC";
            background-color: white;
        }

        nav {
            background-color: #f9f9f9;
            padding: 20px 0;
            text-align: center;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
        }

        nav a {
            text-decoration: none;
            color: #333;
            margin: 0 15px;
            font-weight: bold;
            transition: color 0.3s ease-in-out;
        }

        nav a:hover {
            color: #555;
        }


        header h1 {
            margin: 20px 0;
            text-align: center;
            color: #333;

            font-size: 2em;
            margin-bottom: 40px;
        }

        /* 上传文件表单样式 */
        form {
            display: flex;
            flex-direction: column;
            width: 50%;
            margin: 40px auto;
            align-items: center;
            padding: 20px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            background-color: #f9f9f9;
        }

        form div {
            margin: 10px;
        }

        /* 上传文件输入框样式 */
        input[type=file] {
            padding: 8px;
            color: #333;
            border: 1px solid #ccc;
            border-radius: 4px;
            background-color: #fff;
            cursor: pointer;
        }

        /* 上传按钮样式 */
        input[type=submit] {
            color: #f5f5f5;
            background-color: #333;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            border-radius: 5px;
            font-weight: bold;
        }

        /* 上传按钮鼠标悬停效果 */
        input[type=submit]:hover {
            background-color: #555;
            color: #fff;
        }

        /* 弹窗基本样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
            padding-top: 100px;
        }

        .modal-content {
            background-color: #f9f9f9;
            margin: auto;
            padding: 20px;
            border: 1px solid #ccc;
            width: 80%;
            max-width: 500px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        footer {
            color: #333;
            background-color: #f9f9f9;
            text-align: center;
            padding: 20px;
            margin-top: 100px;
        }
    </style>
</head>

<body>
    <nav>
        <a href="/">首页</a>
        <a href="/upload.html">上传</a>
        <a href="#" id="search_link">搜索</a>
        <a href="#">登录</a>
        <a href="#">竞赛</a>
        <a href="#">讨论</a>
        <a href="#">求职</a>
    </nav>

    <header>
        <h1 id="status">欢迎上传云备份文件</h1>
    </header>

    <div>
        <form action="/upload" method="POST" enctype="multipart/form-data">
            <div><input type="file" name="file"></div>
            <div><input type="submit" value="Upload"></div>
        </form>
    <div>

    <div id="error_modal" class="modal">
        <div class="modal-content">
            <p id="error_msg"></p>
        </div>
    </div>

    <footer>
        <p>@yourfriendyo</p>
    </footer>
</body>

<script>
    document.addEventListener('DOMContentLoaded', function(event) {
        // 获取表单元素
        const form = document.querySelector('form');

        // 监听表单的提交事件
        form.addEventListener('submit', function(event) {
            event.preventDefault(); // 阻止表单默认提交行为

            const data = new FormData(this); // 使用FormData对象来收集文件数据

            // 使用fetch API提交数据
            fetch('/upload', {
                method: 'POST',
                body: data,
            })
            .then(response => response.json())
            .then(data => {
                const h1 = document.querySelector('header h1'); // 检查result并更新h1标题

                if (data.result === true) {
                    h1.textContent = '文件上传成功';
                } else {
                    h1.textContent = '文件上传失败';
                }

                count2Redirect('/'); // 实现倒计时自动跳转
            })
            .catch(error => {
                showError('Error upload: ' + error.message);
                console.error('Error:', error);
            });
        });
    });

    function count2Redirect(url) {
        let seconds = 3; // 设定倒计时的秒数
        const h1 = document.querySelector('header h1');
        const text = h1.textContent;

        const intervalId = setInterval(function() {
            if (seconds === 0) {
                clearInterval(intervalId);
                window.location.href = url; // 实现页面跳转
            } else {
                h1.textContent = `${text}，${seconds}秒后返回主页`;
                seconds--;
            }
        }, 1000);
    }

    function showError(message) {
        const modal = document.getElementById('error_modal');
        const errorMessage = document.getElementById('error_msg');
        errorMessage.textContent = message; // 按需设置错误信息
        modal.style.display = "block"; // 显示弹窗

        // 点击窗口外部隐藏弹窗
        window.onclick = function (event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    }
</script>

</html>